package data;

import java.util.ArrayList;

import models.AutoSite;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class AutoSiteData {
	
	private static Objectify ofy;
	
	public static void insert(AutoSite autoSite) {
		init();
		ofy.put(autoSite);
	}
	
	public static AutoSite getById(String autoSiteId) {
		init();
		AutoSite autoSite  = new AutoSite();
		try {
			autoSite = ofy.get(new Key<AutoSite>(AutoSite.class, autoSiteId));
		} catch (Exception e) {
			autoSite = null;
		}
		return autoSite;
	}
	
	public static ArrayList<AutoSite> getByBlogId(String blogId) {
		init();
		ArrayList<AutoSite> sites= new ArrayList<AutoSite>();
		
		Query<AutoSite> q = ofy.query(AutoSite.class).filter("blogId", blogId);
		for (AutoSite autoSite: q) {
			sites.add(autoSite);
		}
		
		return sites;
	}
	
	public static ArrayList<AutoSite> getAlls() {
		init();
		ArrayList<AutoSite> sites = new ArrayList<AutoSite>();
		
		Query<AutoSite> q = ofy.query(AutoSite.class).limit(40);
		
		for (AutoSite autoSite: q) {
			sites.add(autoSite);
		}
		
		return sites;
	}
	
	public static void delete(AutoSite autoSite) {
		init();
		ofy.delete(autoSite);
	}
	
	private static void init() {
		try {
			ObjectifyService.register(AutoSite.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		ofy = ObjectifyService.begin();
	}
}
